<?php

namespace Aanmelden\Model;

class WachtwoordVergeten {
    private $_gegevens = [];
    private $_boodschap = '';
    private $_foutenLijst = [];
    private $_json = ['resultaat'=>true, 'boodschap'=>''];
    private $_dbVerbinding = null;
    private $_validatie = null;
    private $_idPersoon = 0;
    private $_naamPersoon = '';
    private $_aCode = '';
    
    public function bijwerken() {
        $this->_gegevens = $_POST;
        
        if (!is_array($this->_gegevens)) {
            $this->_json['resultaat'] = false;
            $this->_json['boodschap'] = 'Geen data';
            return $this->_json;
        }
        
        $this->_validatie = new \Validatie();
        
        $this->_dbVerbinding = \db::GET();
        
        $this->_persoonValideer();
        if (!$this->_json['resultaat']) {
            $this->_json['boodschap'] = 'Foute gegevens....';
            return $this->_json;
        }
        
        $this->_persoonBestaat();
        if (!$this->_json['resultaat']) {
            $this->_json['boodschap'] = 'Persoonsgegevens niet correct....';
            return $this->_json;
        }
        
        $this->_persoonActiveercode();
        $this->_persoonStuurMail();
        if ($this->_json['reultaat']) {
            $this->_json['boodschap'] = 'Je account werd met succes gereset...  Een e-mail met de activatielink werd naar u verstuurd...';
        }
        return $this->_json;
    }
    
    private function _persoonValideer() {
        $this->_gegevens['email'] = trim($this->_gegevens['email']);
        if (!$this->_validatie->email($this->_gegevens['email'])) {
            array_push($this->_foutenLijst,'e-mail');
            $this->_json['resultaat'] = false;
        }
        
        $this->_gegevens['login'] = trim($this->_gegevens['login']);
        if (!$this->_validatie->tekst($this->_gegevens['login'],2)) {
            array_push($this->_foutenLijst,'login');
            $this->_json['resultaat'] = false;
        }
    }
    
    private function _persoonBestaat() {
        $dbSql = "SELECT id,CONCAT_WS(' ',voornaam,naam) as naam, code, status FROM persoon WHERE email=:email AND login=:login";
        try {
            $dbStmt = $this->_dbVerbinding->prepare($dbSql);
            $dbStmt->execute([
                ':email'=>$this->_gegevens['email'],
                ':login'=>$this->_gegevens['login']
            ]);
            if ($dbStmt->rowCount() == 0) {
                $this->_json['resultaat'] = false;
            } else {
                $dbRij = $dbStmt->fetch(\PDO::FETCH_ASSOC);
                $this->_idPersoon = $dbRij['id'];
                $this->_naamPersoon = $dbRij['naam'];
                if ($dbRij['status'] == 1) {
                    $this->_aCode = $dbRij['code'];
                }
            }
        } catch (PDOException $ex) {
            $this->_json['resultaat'] = false;
        }
    }
    
    private function _persoonActiveercode() {
        if (empty($this->_aCode)) {
            $dbSql = "SELECT 1 FROM persoon WHERE code=:code";
            try {
                $dbStmt = $this->_dbVerbinding->prepare($dbSql);
                do {
                    $codeTest = true;
                    $this->_aCode = sha1(microtime());
                    $dbStmt->execute([':code'=>$this->_aCode]);
                    if ($dbStmt->rowCount() == 0) {
                        $codeTest = false;
                    }
                } while ($codeTest);
                $dbSql = "UPDATE persoon SET code=:code, status=1 WHERE id=:idpersoon";
                $dbStmt = $this->_dbVerbinding->prepare($dbSql);
                $dbStmt->execute([':code'=>$this->_aCode,
                                  ':idpersoon'=>$this->_idPersoon]);
            } catch (Exception $ex) {
                $this->_json['resultaat'] = false;
            }
        }
    }
    
    private function _persoonStuurMail() {
        $aLink = \Instelling::GET('website.root').'Aanmelden/Activeer/index/0/?acode='.$this->_aCode;
        $mail = new \MimeMessage();
        $html = <<<EOD
             <h1><small>Beste </small>{$this->_naamPersoon}</h1>
             <p>Activeer je account opnieuw door op volgende koppeling te klikken: <a href="{$aLink}">activeer mijn account</a></p>
             <p>met vriendelijk groet,<br>het team</p>
EOD;
        $mail->msgFrom(\Instelling::GET('website.mail.from'));
        $mail->msgReplyTo(\Instelling::GET('website.mail.replyto'));
        $mail->msgSubject('Heractiveren van je account');
        $mail->msgTo($this->_gegevens['email']);
        $mail->msgHtml($html);
        if ($mail->sendMessage()) {
            
        } else {
            $this->_json['boodschap'] = 'het versturen van de activatiemail heeft gefaald';
            $this->_json['resultaat'] = false;
        }
    }
}
